

from midi.MidiOutStream import MidiOutStream
import globals
import player
import enemy
import tunnel
import enemyMidi
import friendMidi
#Panda specific imports for this module
from pandac.PandaModules import *
#To handle events
from direct.showbase.DirectObject import DirectObject
from random import random



from pandac.PandaModules import Point3 #@UnresolvedImport


class myMidiNote:
    def __init__(self):
            self.start = None
            self.ob = None
            self.channel = 0.0
            self.velocity = 0.0
            self.pitch = 0.0
            self.duration = 0.0
            self.name = "empty"

    def __repr__(self):
            return str(self.start)+"-"+str(self.channel)+"-"+str(self.velocity)+"-"+str(self.pitch)+"-"+str(self.duration)

    def create(self, name,time,duration,channel,pitch,vel):
            self.name = name
            self.channel = channel
            self.velocity = vel
            self.pitch = pitch
            self.start = time
            self.duration = duration

track_as_midi = 0.0
mSecondsQuarterNote = 0.0
SecondsPerTick = 0.0
ticksQuarterNote = 0.0

def createEnemies(midinotes, midichannels):
    for it in midinotes:
            if midichannels.has_key(it.channel):
                if globals.groMODEL[it.channel]['kindofmidi'] == 'friend':
                    tmpEnemy = friendMidi.FriendMidi()
                    tmpName = 'bueno' + str(it.start)
                else:
                    tmpEnemy = enemyMidi.EnemyMidi()
                    tmpName = 'malo' + str(it.start)
                tmpEnemy.time = it.start 
                tmpEnemy.duration = it.duration 
                tmpEnemy.offset = globals.groMODEL[it.channel]['offset']
                tmpEnemy.model = globals.groMODEL[it.channel]['model']
                tmpEnemy.anim = globals.groMODEL[it.channel]['anim']
                tmpEnemy.type = globals.groMODEL[it.channel]['type']
                tmpEnemy.snd = globals.groMODEL[it.channel]['snd']
                tmpEnemy.coltype =globals.groMODEL[it.channel]['coltype']
                tmpEnemy.colx = globals.groMODEL[it.channel]['colx']
                tmpEnemy.coly =globals.groMODEL[it.channel]['coly']
                tmpEnemy.colz =globals.groMODEL[it.channel]['colz']
                tmpEnemy.channel = it.channel
                tmpEnemy.volume = it.velocity
                tmpEnemy.key= it.pitch
                tmpEnemy.scorePoints = 100
                tmpEnemy.channel = it.channel
                tmpEnemy.velocity = it.velocity
                tmpEnemy.pitch = it.pitch
                tmpEnemy.init()
                print tmpEnemy.channel, tmpEnemy.anim, tmpEnemy.type, tmpEnemy.coltype,tmpEnemy.time
                tmpName = 'malo'+str(it.start)+str(it.channel)
                tmpEnemy.actor.setTag('enemy',tmpName)
                globals.ENEMIES[tmpName] = tmpEnemy
                globals.ENEMIES_MOUSE[tmpName] = tmpEnemy
                tmpEnemy = None
                
class MidiToText(MidiOutStream):



    """
    This class renders a midi file as text. It is mostly used for debugging
    """

    #############################
    # channel events


    def channel_message(self, message_type, channel, data):
        """The default event handler for channel messages"""
        print 'message_type:%X, channel:%X, data size:%X' % (message_type, channel, len(data))


    def note_on(self, channel=0, note=0x40, velocity=0x40):
        global SecondsPerTick
        midinote = myMidiNote()
        name = str(channel)+str(self.abs_time())
        time = float(self.abs_time()) * SecondsPerTick
        rel =  float(self.rel_time()) * SecondsPerTick
        midinote.create(name, time,rel,int(channel),float(note),float(velocity))
        globals.NOTESGRO.append(midinote)
        globals.NOTESGRO.sort(key = lambda myMidiNote:myMidiNote.start)
        #print 'note_on  - ch:%02X,  note:%02X,  vel:%02X time:%s' % (channel, note, velocity, self.abs_time())


    def note_off(self, channel=0, note=0x40, velocity=0x40):
        pass
        #print 'note_off - ch:%02X,  note:%02X,  vel:%02X time:%s' % (channel, note, velocity, self.rel_time())

    def aftertouch(self, channel=0, note=0x40, velocity=0x40):
        print 'aftertouch', channel, note, velocity


    def continuous_controller(self, channel, controller, value):
        print 'controller - ch: %02X, cont: #%02X, value: %02X' % (channel, controller, value)


    def patch_change(self, channel, patch):
        print 'patch_change - ch:%02X, patch:%02X' % (channel, patch)


    def channel_pressure(self, channel, pressure):
        print 'channel_pressure', channel, pressure


    def pitch_bend(self, channel, value):
        print 'pitch_bend ch:%s, value:%s' % (channel, value)



    #####################
    ## Common events


    def system_exclusive(self, data):
        print 'system_exclusive - data size: %s' % len(date)


    def song_position_pointer(self, value):
        print 'song_position_pointer: %s' % value


    def song_select(self, songNumber):
        print 'song_select: %s' % songNumber


    def tuning_request(self):
        print 'tuning_request'


    def midi_time_code(self, msg_type, values):
        print 'midi_time_code - msg_type: %s, values: %s' % (msg_type, values)



    #########################
    # header does not really belong here. But anyhoo!!!

    def header(self, format=0, nTracks=1, division=96):
        global ticksQuarterNote

        ticksQuarterNote = division
        print ticksQuarterNote
        print 'format: %s, nTracks: %s, division: %s' % (format, nTracks, division)
        
        print '----------------------------------'
        print ''

    def eof(self):
        print 'End of file'


    def start_of_track(self, n_track=0):
        print 'Start - track #%s' % n_track


    def end_of_track(self):
        print 'End of track'
        print ''



    ###############
    # sysex event

    def sysex_event(self, data):
        print 'sysex_event - datasize: %X' % len(data)


    #####################
    ## meta events

    def meta_event(self, meta_type, data):
        print 'undefined_meta_event:', meta_type, len(data)


    def sequence_number(self, value):
        print 'sequence_number', number


    def text(self, text):
        print 'text', text


    def copyright(self, text):
        print 'copyright', text


    def sequence_name(self, text):
        print 'sequence_name:', text


    def instrument_name(self, text):
        print 'instrument_name:', text


    def lyric(self, text):
        print 'lyric', text


    def marker(self, text):
        print 'marker', text


    def cuepoint(self, text):
        print 'cuepoint', text


    def midi_ch_prefix(self, channel):
        print 'midi_ch_prefix', channel


    def midi_port(self, value):
        print 'midi_port:', value


    def tempo(self, value):
        global ticksQuarterNote
        global SecondsPerTick
        mSecondsQuarterNote = float(value)
        SecondsPerTick = mSecondsQuarterNote / (ticksQuarterNote * 1000000.0)
        print 'tempo:', value, SecondsPerTick,ticksQuarterNote

    def smtp_offset(self, hour, minute, second, frame, framePart):
        print 'smtp_offset', hour, minute, second, frame, framePart


    def time_signature(self, nn, dd, cc, bb):
        print 'time_signature:', nn, dd, cc, bb


    def key_signature(self, sf, mi):
        print 'key_signature', sf, mi


    def sequencer_specific(self, data):
        print 'sequencer_specific', len(data)

